package com.yjx.managedemo.handler;


import com.yjx.managedemo.util.ThreadLocal;
import org.springframework.web.servlet.HandlerInterceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

/**
 * @Author: OlderHard
 * @CreateTime: 2025-01-02
 * @Description: 登录拦截器
 * @Version: 1.0
 */

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
        HttpSession session = request.getSession();
        Object token =session.getAttribute("token");

        if (token == null) {
            response.sendRedirect("/admin/toLogin");
            return false;
        }
        ThreadLocal.set("token", token);
        return true;
    }

    @Override
    public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
        ThreadLocal.remove();
    }
}
